package com.ws;

import cn.hutool.core.collection.ConcurrentHashSet;

import javax.websocket.Session;
import java.util.HashMap;

public class WebSocketSync {
    /**
     * key:currentSyncId value:chatId
     */
    public static final HashMap<Session, ConcurrentHashSet<Long>> syncMap = new HashMap<>();

    public static void startSync(Session userSession) {
        syncMap.put(userSession, new ConcurrentHashSet<>());
    }

    public static void removeSync(Session userSession) {
        syncMap.remove(userSession);
    }

    public static void addSync(Session userSession, Long chatId) {
        syncMap.get(userSession).add(chatId);
    }

    /**
     * 当前用户的session是否被使用过
     * @param userSession
     * @return
     */
    public static boolean isUsed(Session userSession, Long chatId) {
        return syncMap.get(userSession).contains(chatId);
    }

    public static boolean hasSync(Session userSession) {
        return syncMap.containsKey(userSession);
    }
}
